// page_two/xiaosxx/xiaosxx.js
let app = getApp()
let yuming = app.globalData.yuming
let mobile = app.globalData.mobile
let chaninese = app.globalData.chaninese
var shuzong = []
var shu = []
const request = require('../../config/request');
Page({
  ...request,
  /**
   * 页面的初始数据
   */
  data: {
    yuming: yuming,
    heights: "",
    title: '特药流向',
    buttonClicked: false,
    huan: ['销售信息', '添加销售信息'],
    currtab: 0,
    page: 1,
    // xsxxList: [{
    //   createtime: '2022-4-21',
    //   ypname: '甲硝唑胶囊',
    //   gyr: '小夫',
    //   phone: '15924454849',
    //   sfzzm: `${yuming}static/applet/images/xiaofei/images/linshi/wyfw.jpg`,
    //   sfzfm: `${yuming}static/applet/images/xiaofei/images/linshi/yxc.jpg`,
    //   hst: `${yuming}static/applet/images/xiaofei/images/linshi/wem.jpg`,
    //   zz: '发热,咳嗽',
    //   yptype: '口服',
    //   gydate: '2022-04-21',
    //   xsr: '赵大猫',
    //   ypt: `${yuming}static/applet/images/xiaofei/images/linshi/wyn.jpg`,
    // }],
    xsxxList: [],
    ypnameindex: 0,
    // ypnamelist: ['甲硝唑胶囊', '甲硝唑胶囊', '甲硝唑胶囊'],
    ypnamelist: [],
    ypname: '请选择药品',
    gyr: '',
    phone: '',
    sfz: '',
    wusfz: '',
    jkm: '',
    wujkm: '',
    hszm: '',
    wuhszm: '',
    zz: '',
    yptypeindex: 0,
    yptypelist: ['麻黄碱', '处方药'],
    yptype: '请选择药品类型',
    gydate: '请选择购药日期',
    xsrindex: 0,
    xsrlist: [],
    xsr: '请选择销售人',
    ypt: '',
    wuypt: '',
    zzList: [{
        checked: false,
        value: '咳嗽',
      },
      {
        checked: false,
        value: '头痛',
      },
      {
        checked: false,
        value: '咽痛',
      },
      {
        checked: false,
        value: '鼻塞',
      },
      {
        checked: false,
        value: '发热',
      }
    ],
    date: '选择时间', //开始
    dateTime: '',
    istime: false, //弹出时间筛选
    gongwu: false,
  },
  tan() {
    this.setData({
      istime: !this.data.istime
    })
  },
  queren() {
    this.getxsxxList();
    this.setData({
      istime: !this.data.istime
    })
  },
  // 选择开始时间
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value,
      dateTime: e.detail.value
    })
  },
  //多选
  userCheck: function (e) {
    let index = e.currentTarget.dataset.id; //获取用户当前选中的索引值
    let Box = this.data.zzList
    if (Box[index].checked) {
      this.data.zzList[index].checked = false;
    } else {
      this.data.zzList[index].checked = true;
    }
    this.setData({
      zzList: this.data.zzList
    })
    // 返回用户选中的值
    let value = Box.filter((item, index) => {
      return item.checked == true;
    })
    let xditem = ''
    value.forEach((item, index) => {
      xditem += item.value + ','
    })
    if (xditem.slice(-1) == ',') {
      xditem = xditem.slice(0, xditem.length - 1);
    }
    this.setData({
      zz: xditem
    })
    console.log(this.data.zzList)
  },
  qies(e) {
    shuzong = []
    shu = []
    var index = e.currentTarget.dataset.currtab
    this.data.zzList.forEach(item => {
      item.checked = false
    })
    this.setData({
      currtab: index,
      page: 1,
      ypname: '请选择药品',
      gyr: '',
      phone: '',
      sfz: '',
      wusfz: '',
      jkm: '',
      wujkm: '',
      hszm: '',
      wuhszm: '',
      zz: '',
      yptype: '请选择药品类型',
      gydate: '请选择购药日期',
      xsr: '请选择销售人',
      ypt: '',
      wuypt: '',
      date: '选择时间', //开始
      dateTime: '',
      gongwu: false,
      zzList: this.data.zzList
    })
    if (this.data.currtab == 0) {
      this.getxsxxList();
    }
  },
  // 查看更多
  lookmore() {
    shuzong = this.data.xsxxList
    this.setData({
      page: (this.data.page) + 1
    })
    this.getxsxxList();
  },
  // 销售信息列表接口
  getxsxxList() {
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    let that = this
    this.myRequest(yuming + 'Yiyao/yiyaoXiaoshouList', {
      shopId: wx.getStorageSync('shopId'),
      page: that.data.page,
      daytime: that.data.dateTime
    }, "POST").then(res => {
      if (res.data.code == 200) {
        wx.hideLoading();
        shu = res.data.data
        if (that.data.page != 1) {
          if (shu.length != 10) {
            that.setData({
              gongwu: true
            })
          }
        }
        that.setData({
          xsxxList: shuzong.concat(shu)
        })
      }
    })
  },
  getypnameList() {
    let that = this
    this.myRequest(yuming + 'Yiyao/yiyaoTeshuyaoAll', {
      shopId: wx.getStorageSync('shopId')
    }, "POST").then(res => {
      if (res.data.code == 200) {
        let ypname = [];
        res.data.data.forEach(item => {
          ypname.push(item.name)
        })
        that.setData({
          ypnamelist: ypname
        })
        console.log(that.data.ypnamelist)
      }
    })
  },
  getxsrList() {
    let that = this
    this.myRequest(yuming + 'shop/shopSelectStaff', {
      shopId: wx.getStorageSync('shopId')
    }, "POST").then(res => {
      if (res.data.code == 200) {
        let xsr = [];
        res.data.result.forEach(item => {
          xsr.push(item.Name)
        })
        that.setData({
          xsrlist: xsr
        })
        console.log(that.data.xsrlist)
      }
    })
  },
  // 药品名称
  ypname(e) {
    let index = e.detail.value
    this.setData({
      ypname: this.data.ypnamelist[index],
    });
  },
  // 购药人
  gyr(e) {
    this.setData({
      gyr: e.detail.value
    })
  },
  // 电话
  phone(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  // 症状
  zz(e) {
    this.setData({
      zz: e.detail.value
    })
  },
  // 药品类型
  yptype(e) {
    let index = e.detail.value
    this.setData({
      yptype: this.data.yptypelist[index],
    });
  },
  // 购药日期
  gydate: function (e) {
    this.setData({
      gydate: e.detail.value
    })
  },
  // 销售人
  xsr(e) {
    let index = e.detail.value
    this.setData({
      xsr: this.data.xsrlist[index],
    });
  },
  // 上传
  chuan(e) {
    var aaa = e.currentTarget.dataset.aaa
    var that = this;
    //选取图片
    this.myChooseImage(1).then(res => {
      const tempFilePaths = res.tempFilePaths
      this.myUploadImg(tempFilePaths[0], 'file').then(res => {
        var data = JSON.parse(res.data)
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
        var urls = data.httpUrl + data.imgUrl
        // 身份证
        if (aaa == 1) {
          that.setData({
            sfz: urls,
            wusfz: data.imgUrl
          })
        }
        // 健康码
        if (aaa == 2) {
          that.setData({
            jkm: urls,
            wujkm: data.imgUrl
          })
        }
        // 核酸证明
        if (aaa == 3) {
          that.setData({
            hszm: urls,
            wuhszm: data.imgUrl
          })
        }
        // 药品图
        if (aaa == 4) {
          that.setData({
            ypt: urls,
            wuypt: data.imgUrl
          })
        }
      })
    })
  },
  // 点击放大
  fangdas(e) {
    var img = e.currentTarget.dataset.img
    wx.previewImage({
      urls: [img]
    })
  },
  // 验证
  ti() {
    if (this.data.ypname == '请选择药品') {
      wx.showToast({
        title: '请选择药品',
        icon: 'none'
      })
    } else if ((chaninese.test(this.data.gyr))) {
      wx.showToast({
        title: '只允许输入中文',
        icon: 'none'
      })
      this.setData({
        gyr: ''
      })
    } else if (this.data.gyr == '') {
      wx.showToast({
        title: '请输入购药人',
        icon: 'none'
      })
    } else if (this.data.gyr.length < 2) {
      wx.showToast({
        title: '购药人名称格式错误',
        icon: 'none'
      })
    } else if (this.data.phone == '') {
      wx.showToast({
        title: '请输入电话',
        icon: 'none'
      })
    } else if (mobile.test(this.data.phone) == false) {
      wx.showToast({
        title: '请输入正确的电话号码',
        icon: 'none'
      })
    } else if (this.data.wusfz == '') {
      wx.showToast({
        title: '请上传身份证照片',
        icon: 'none'
      })
    } else if (this.data.wujkm == '') {
      wx.showToast({
        title: '请上传健康码照片',
        icon: 'none'
      })
    } else if (this.data.wuhszm == '') {
      wx.showToast({
        title: '请上传核酸证明',
        icon: 'none'
      })
    } else if (this.data.zz == '') {
      wx.showToast({
        title: '请选择症状',
        icon: 'none'
      })
    } else if (this.data.yptype == '请选择药品类型') {
      wx.showToast({
        title: '请选择药品类型',
        icon: 'none'
      })
    } else if (this.data.gydate == '请选择购药日期') {
      wx.showToast({
        title: '请选择购药日期',
        icon: 'none'
      })
    } else if (this.data.xsr == '请选择销售人') {
      wx.showToast({
        title: '请选择销售人',
        icon: 'none'
      })
    } else if (this.data.wuypt == '') {
      wx.showToast({
        title: '请上传药品图',
        icon: 'none'
      })
    } else {
      this.que()
    }
  },
  // 添加销售信息
  que() {
    let that = this
    that.setData({
      buttonClicked: true
    })
    that.data.zzList.forEach(item => {
      item.checked = false
    })
    this.myRequest(yuming + 'Yiyao/yiyaoXiaoshouAdd', {
      shopId: wx.getStorageSync('shopId'),
      name: that.data.ypname,
      maiyaoname: that.data.gyr,
      mobile: that.data.phone,
      shenfen_img: that.data.wusfz,
      jiankang_img: that.data.wujkm,
      hesuan_img: that.data.wuhszm,
      zhengzhuang: that.data.zz,
      catename: that.data.yptype,
      maiyaotime: that.data.gydate,
      xiaoshouname: that.data.xsr,
      yaopin_img: that.data.wuypt
    }, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        that.getxsxxList();
        setTimeout(function () {
          that.setData({
            currtab: 0,
            buttonClicked: false,
            ypname: '请选择药品',
            gyr: '',
            phone: '',
            sfz: '',
            wusfz: '',
            jkm: '',
            wujkm: '',
            hszm: '',
            wuhszm: '',
            zz: '',
            yptype: '',
            gydate: '请选择购药日期',
            xsr: '请选择销售人',
            ypt: '',
            wuypt: '',
            date: '选择时间', //开始
            dateTime: '',
            zzList: that.data.zzList
          })
        }, 1000)
      } else {
        that.setData({
          buttonClicked: false
        })
      }

    })

  },
  // 删除
  shanchu(e) {
    var id = e.currentTarget.dataset.id
    var that = this
    wx.showModal({
      title: '您确定要删除吗？',
      cancelText: '取消',
      confirmText: '确定',
      success: function (res) {
        if (res.confirm) {
          console.log("确定")
          that.dele(id)
        } else {
          console.log("取消")
        }
      }
    })
  },
  // 删除接口
  dele(id) {
    let that = this
    this.myRequest(yuming + 'Yiyao/yiyaoXiaoshouDel', {
      id: id
    }, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        shuzong = [];
        shu = [];
        that.getxsxxList();
      }
    })
  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      shopType: options.shopType,
      title: options.title
    })
    this.getxsxxList();
    this.getypnameList();
    this.getxsrList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})